package exfist;

/**
 * A tree structure used to describe the current state of a player at any given time
 * @author brianmaccarthy
 *
 */
public class State_Tree
{
	protected Control_Node base_state;
	public State_Tree()
	{
		this(null);
	}
	/**
	 * constructs a tree with a root node
	 * @param base the root of the tree
	 */
	public State_Tree(Control_Node base)
	{
		base_state = base;
	}
	/**
	 * traverses the branch of a tree
	 * @param current the current node
	 * @param p the player whose tree it is
	 */
	protected void traverseState(Node current,Player p)
	{
		p.setCurrentNode(current);
		if(current.node_name.equals("base_state"))
		{
		}
		else
			p.updatePos(current);
		try 
		{
			Thread.sleep(40);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(current.check_collision==true)
		{
			// System.out.println(p.collision(p.opponent, p.getX()-p.opponent.getX(), p.getX()));
			if(p.collision(p.opponent, p.getX()-p.opponent.getX(), p.getX())==true)
			{
				p.opponent.fall();
				//p.winner();
			}
		}
		if(current.hasChild()==true)
		{
				traverseState(current.getChild(),p);
		}
	}
	/**
	 * adds a new node to the tree
	 * @param parent the nodes parent
	 * @param newChild the node being added
	 */
	protected void addNode(Node parent,Node newChild)
	{
		parent.setChild(newChild);
		parent.has_child=true;
	}
	/**
	 * adds a new node to the control node
	 * @param parent the control node
	 * @param newChild the child
	 */
	protected void addNewState(Control_Node parent,Node newChild)
	{
		parent.addChild(newChild);
	}
	
}
